package com.m3958.firstgwt.server.service;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import com.google.inject.Singleton;

@Singleton
public class JedisPoolService {
	
	private JedisPool pool;
	
	public JedisPoolService(){
		pool = new JedisPool(new JedisPoolConfig(), "localhost");
	}
	
	public Jedis getInstance(){
		try {
			return pool.getResource();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void putBack(Jedis jedis){
		pool.returnResource(jedis);
	}
}

//Jedis jedis = pool.getResource();
//try {
//  /// ... do stuff here ... for example
//  jedis.set("foo", "bar");
//  String foobar = jedis.get("foo");
//  jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike"); 
//  Set<String> sose = jedis.zrange("sose", 0, -1);
//} finally {
//  /// ... it's important to return the Jedis instance to the pool once you've finished using it
//  pool.returnResource(jedis);
//}
///// ... when closing your application:
//pool.destroy();